package com.apinji.wxpay.sdk;

import com.apinji.common.plugin.StrUtils;

import java.io.*;
import java.util.Properties;

public class WXPayConfigImpl extends WXPayConfig{
    public static final String FILE_NAME = "wxpay_sdk.properties";

    private String certPath;
    private String appID;
    private String mchID;
    private String key;
    private String backPayUrl;

    private byte[] certData;
    private static WXPayConfigImpl INSTANCE;

    private WXPayConfigImpl() throws Exception{
        loadPropertiesFromSrc();
        //
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }


    /**
     * 从classpath路径下加载配置参数
     */
    public void loadPropertiesFromSrc() {
        InputStream in = null;
        Properties properties = null;
        try {
            in = WXPayConfigImpl.class.getClassLoader().getResourceAsStream(FILE_NAME);
            if (null != in) {
                properties = new Properties();
                try {
                    properties.load(in);
                } catch (IOException e) {
                    throw e;
                }
            } else {
                return;
            }
            loadProperties(properties);
        } catch (IOException e) {
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * 根据传入的 {@link #loadProperties(Properties)}对象设置配置参数
     *
     * @param pro
     */
    public void loadProperties(Properties pro) {
        String classPath = this.getClass().getResource("/").getPath();
        if("\\".equals(classPath)) {
            classPath = classPath.substring(1, classPath.length());
        }
        classPath = classPath.replace("/", File.separator) + File.separator;

        String value = null;
        value = pro.getProperty("wxsdk.signCert.path");
        if (!StrUtils.isEmpty(value)) {
            this.certPath = classPath + value.trim().replace("/", File.separator);
        }

        value = pro.getProperty("wxsdk.appID");
        if (!StrUtils.isEmpty(value)) {
            this.appID = value.trim();
        }

        value = pro.getProperty("wxsdk.mchID");
        if (!StrUtils.isEmpty(value)) {
            this.mchID = value.trim();
        }

        value = pro.getProperty("wxsdk.key");
        if (!StrUtils.isEmpty(value)) {
            this.key = value.trim();
        }

        value = pro.getProperty("wxsdk.pay.backUrl");
        if (!StrUtils.isEmpty(value)) {
            this.backPayUrl = value.trim();
        }
    }


    public static WXPayConfigImpl getInstance() throws Exception{
        if (INSTANCE == null) {
            synchronized (WXPayConfigImpl.class) {
                if (INSTANCE == null) {
                    INSTANCE = new WXPayConfigImpl();
                }
            }
        }
        return INSTANCE;
    }

    public String getAppID() {
        return appID;
    }

    public String getMchID() {
        return mchID;
    }

    public String getKey() {
        return key;
    }

    public String getBackPayUrl() {
        return backPayUrl;
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }


    public int getHttpConnectTimeoutMs() {
        return 2000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }

    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }

    public String getAlternateDomain() {
        return "api2.mch.weixin.qq.com";
    }

    @Override
    public int getReportWorkerNum() {
        return 1;
    }

    @Override
    public int getReportBatchSize() {
        return 2;
    }
}
